<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form ref="formRef" :model="formData">
      <el-form-item label="分级变更：" prop="levelChange">
        <el-checkbox-group v-model="formData.levelChange">
          <el-checkbox label="有变更" value="1" />
          <el-checkbox label="无变更" value="2" />
        </el-checkbox-group> </el-form-item
      ><el-form-item label="分类变更：" prop="classificationChange">
        <el-checkbox-group v-model="formData.classificationChange">
          <el-checkbox label="有变更" value="1" />
          <el-checkbox label="无变更" value="2" />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="是否只显示重要数据资产：" prop="isOnlyShowImportance">
        <el-radio-group v-model="formData.isOnlyShowImportance" @change="radioChange">
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="重要数据资产：" prop="importanceOperations">
        <el-checkbox-group v-model="formData.importanceOperations">
          <el-checkbox
            v-for="(item, index) in operationList2"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item
        label="待确认数据资产："
        prop="waitOperations"
        v-if="formData.isOnlyShowImportance == 0"
      >
        <el-checkbox-group v-model="formData.waitOperations">
          <el-checkbox
            v-for="(item, index) in operationList"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item
        label="公开数据资产："
        prop="openOperations"
        v-if="formData.isOnlyShowImportance == 0"
      >
        <el-checkbox-group v-model="formData.openOperations">
          <el-checkbox
            v-for="(item, index) in operationList2"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submitForm">确定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import * as assetChangeLogApi from '@/api/assetChangeLog/index'
defineOptions({ name: 'StandardsForm' })

const router = useRouter()
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题

const formData = ref({
  isOnlyShowImportance: '',
  importanceOperations: [],
  waitOperations: [],
  openOperations: [],
  levelChange: [],
  classificationChange: []
})

const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  dialogTitle.value = '日志范围设置'
  // resetForm()
  getConfig()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const operationList = [
  {
    label: '新增',
    value: '新增'
  },
  {
    label: '删除',
    value: '删除'
  },
  {
    label: '修改',
    value: '修改'
  },
  {
    label: '撤回',
    value: '撤回'
  },
  {
    label: '审核通过',
    value: '审核通过'
  },
  {
    label: '审核拒绝',
    value: '审核拒绝'
  },
  {
    label: '提交审核',
    value: '提交审核'
  },
  {
    label: '迁移',
    value: '迁移'
  }
]

const operationList2 = [
  {
    label: '删除',
    value: '删除'
  },
  {
    label: '修改',
    value: '修改'
  },
  {
    label: '审核通过',
    value: '审核通过'
  },
  {
    label: '审核拒绝',
    value: '审核拒绝'
  },
  {
    label: '提交审核',
    value: '提交审核'
  },
  {
    label: '迁移',
    value: '迁移'
  },
  {
    label: '自动降敏',
    value: '自动降敏'
  }
]

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  await assetChangeLogApi.levelHistoryCreateOrUpdateConfig(formData.value)
  message.success('操作成功！')
  dialogVisible.value = false
  emit('success')
}

const getConfig = async () => {
  const data = await assetChangeLogApi.levelHistoryGetConfig()
  formData.value = data || {
    isOnlyShowImportance: '',
    importanceOperations: [],
    waitOperations: [],
    openOperations: [],
    levelChange: [],
    classificationChange: []
  }
}

// watch(
//   () => formData.value.isOnlyShowImportance,
//   () => {
//     formData.value.waitOperations = []
//     formData.value.openOperations = []
//   }
// )

const radioChange = () => {
  formData.value.waitOperations = []
  formData.value.openOperations = []
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    isOnlyShowImportance: '',
    importanceOperations: [],
    waitOperations: [],
    openOperations: [],
    levelChange: [],
    classificationChange: []
  }
  formRef.value?.resetFields()
}
</script>
